import { http } from '@/utils/http';

export type LoginParam = {
  username: string;
  password: string;
  /**
   * 是否自动登录
   */
  isRemember: boolean;
};

export type UserResult = {
  // token
  access_token: string;
  // access_token 的过期时间
  expires_in: number;
  // 刷新 access_token 接口时所需的token
  refresh_token: string;
  token_type: string;
};

/**
 * 登录
 * @param data
 * @returns
 */
export const getLogin = (data: LoginParam) => {
  const postStr = `client_id=test&client_secret=test&grant_type=password&username=${data.username}&password=${data.password}&remember=${data.isRemember}`;
  return http.request<UserResult>('post', '/oauth2/token', {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: postStr,
  });
};
